from django.shortcuts import render,HttpResponse,redirect
from .models import Student
# Create your views here.
def add(request):
    name=request.GET.get('name')
    age=request.GET.get('age')
    sex=request.GET.get('sex')
    add1=Student(name=name,age=age,sex=sex)
    add1.save()
    return redirect('/class/allshow')
def dell(request,allid):
    dell1=''
    try:
        dell1=Student.objects.get(id=allid)
    except:
        pass
    if dell1:
        dell1.delete()
        return redirect('/class/allshow/')
    else:
        return HttpResponse('没有要删除的目标')
def show(request,allid):
    show1=''
    try:
        show1=Student.objects.get(id=allid)
    except:
        pass
    if show1:
        return render(request,'show.html',{'show1':show1})
    else:
        return HttpResponse('没有此资源')
def tfadd(request):
    return render(request,'The_from_add.html')
def allshow(request):
    all1=Student.objects.all()
    return render(request,'allshow.html',{'all1':all1})
def xiugai(request,tid):
    xiu=Student.objects.get(id=tid)
    return render(request,"xiugai.html",{'xiu':xiu})
def modify_the(request):
    id=request.GET.get('id')
    mname=request.GET.get('name')
    mage=request.GET.get('age')
    msex=request.GET.get('sex')
    modify_the=Student.objects.get(id=id)
    modify_the.name=mname
    modify_the.age=mage
    modify_the.sex=msex
    modify_the.save()
    return redirect('allshow')
def sousuo(request):
    so=request.GET.get('so')
    sdu=Student.objects.raw('select * from class_student where name like "%{}%"'.format(so))

    return render(request,'cha.html',{"sdu":sdu})